<?php
	class WishListMgr
	{
		private $dbc;
		
		 public function __construct($dbc = null)
	     {
	            if (isset($dbc))
	            {
	                $this->dbc = $dbc;
	            }
	     }
		        
		public function addToWishList($uid,$pid,$qty)
		{	
			 $q ="SELECT id FROM wishlist WHERE user_session_id='$uid' AND product_type='other' AND product_id=$pid"; 
			 $r = mysqli_query($this->dbc, $q);
			 $num =mysqli_num_rows($r);
			 if($num == 1) 
			 {
			 	$row = mysqli_fetch_array($r,MYSQLI_NUM);	
			 	$cid=$row[0];
			 	$this->updateWishListByID($cid,$qty);
			 }
			 else
			 {
			 	$this->insertWishList($uid,$pid,$qty);
			 }
			 	
		}
		
		public function updateWishListByID($cid,$qty)
		{
			$q="UPDATE wishlist SET quantity=quantity+$qty, date_modified=NOW() WHERE id=$cid";
			$r = mysqli_query($this->dbc, $q);
			// For debugging purposes:
			//if (!$r) echo mysqli_error($dbc);
		}
		
		public function updateWishList($uid,$pid,$qty)
		{
			if($qty > 0)
			{
				$q="UPDATE wishlist SET quantity=$qty, date_modified=NOW() WHERE user_session_id='$uid' AND product_type='other' AND product_id='$pid'";
				$r = mysqli_query($this->dbc, $q);
				// For debugging purposes:
				//if (!$r) echo mysqli_error($dbc);
			}
			else 
			{
				$this->removeFromWishList($uid,$pid);
			}
		}
		
		
		public function insertWishList($uid,$pid,$qty)
		{
			$q="INSERT INTO wishlist (user_session_id, product_type, product_id, quantity) VALUES ('$uid', 'other', $pid, $qty)";
			$r = mysqli_query($this->dbc, $q);
			// For debugging purposes:
			//if (!$r) echo mysqli_error($dbc);
		}
		
		public function removeFromWishList($uid,$pid)
		{
			$q="DELETE FROM wishlist WHERE user_session_id='$uid' AND product_type='other' AND product_id=$pid";
			$r = mysqli_query($this->dbc, $q);
			// For debugging purposes:
			//if (!$r) echo mysqli_error($dbc);
		}
		
		public function getShoppingWishListContents($uid)
		{
			$wishlist=array();
			$items =array();
			$sub_total=0;
			$q="SELECT c.quantity,ncp.id, ncp.name, sales.price AS saleprice
				FROM wishlist AS c 
				INNER JOIN product AS ncp ON c.product_id=ncp.id 
				LEFT OUTER JOIN sales ON (sales.product_id=ncp.id AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) ) 
				WHERE c.product_type='other' AND c.user_session_id='$uid'";
			
			$r = mysqli_query($this->dbc, $q);
			
			$num =mysqli_num_rows($r);
			if($num>0)
			{
			$repository=BO::getProductBORepository();
			while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) 
				{
					 $productbo=$repository->getProductBO($row['id']);
					 $_entry=array(); 
					 $_entry['pid']= $row['id'] ;
					 $_entry['name']= $row['name'] ;
					 $_entry['quantity']= $row['quantity'] ;
					 $_entry['image']= $productbo->getImage() ;
					 $_entry['price']= $productbo->getPrice();
					 $_entry['saleprice']= $row['saleprice'] ;			 
					 $_entry['finalprice']= $this->getFinalPrice($productbo->getPrice() , $row['saleprice']) ;
					 $_qty_price= $_entry['finalprice'] * $row['quantity'];
					 $_entry['subtotal']=number_format($_qty_price,2);
					 $items[]=$_entry;
					 	// Add the subtotal to the total:
  					$sub_total += $_qty_price;	
				}
				 $shipping =$this->getShipping($sub_total);
				 $total=$shipping+$sub_total;
				 $wishlist['items']=$items;
				 //$wishlist['subtotal']=number_format($sub_total,2);
				// $wishlist['shipping']=$shipping;
				// $wishlist['total']=number_format($total,2);
			} 
			 return $wishlist;	
		}

		function getFinalPrice($regular, $sales) {	
			// Return the sale price, when appropriate:
			if ((0 < $sales) && ($sales < $regular)) {
				return number_format($sales, 2);
			} else {
				return number_format($regular, 2);
			}
			
		} 
		
// Function for calculating the shipping and handling.
// Takes one argument: the current order total.
// Returns a float.
		public	function getShipping($total = 0) {
				
				// Set the base handling charges:
				$shipping = 0.00;
				
				// Rate is based upon the total:
				if ($total < 500) 
					$shipping=50;
				
				
				// Return the shipping total:
				return number_format($shipping, 2);
				
			} // End of get_shipping() function.
					
		
	     public function getUserSession()
	     {
	     	if (isset($_COOKIE['SESSION'])) {
				return $_COOKIE['SESSION'];
			} else {
				return md5(uniqid('biped',true));
			}
	     	
	     }
	     public function sendCookie($uid)
	     {
	     	setcookie('SESSION', $uid, time()+(60*60*24*30));
	     }
	}